package charts ;
import charts.series.Element;
import charts.series.bars.Horizontal;
import string.Utils;
import global.Global;
import ColorConverter.Color;

class HBar extends Base {
	
		var group: Float;
		var style: Dynamic;
	
	public function new ( json: Dynamic ) {
		super();			
		this.style = {
			values:				[],
			color:				'#3030d0',
			text:				'',		// <-- default not display a key
			font_size:			12,
			tip:				'#val#'
		};
		
		ObjectHelper.merge_2( json, style );
		
		//this.alpha = Number( vals[0] );
		this.color = ColorConverter.toInt(Hex(style.color)); //string.Utils.get_color( style.color );
		this.key = json.text;
		this.font_size = Reflect.field(json,'font-size');
		
		//
		// bars are grouped, so 3 bar sets on one chart
		// will arrange them selves next to each other
		// at each value of X, this.group tell the bar
		// where it is in that grouping
		//
		this.group = 0;
		
		this.values = json.values;
		
		//this.style['onclick'] = json['onclick'];
		//style.onclick = Reflect.field(json,'onclick');
		style.onclick = json.onclick;
		
		this.add_values();
	}
	
	//
	// called from the base object, in this case the
	// value is the X value of the bar and the index
	// is the Y positiont
	//
		override function get_element( index: Int, value: Dynamic ): Element {
		
		var default_style: Dynamic = {
				color:		this.style.color,
				tip:		this.style.tip,
				onclick: 	this.style.onclick
		};
		
		if(Std.is(value,Float))
			default_style.top = value;
		else
			ObjectHelper.merge_2( value, default_style );
			
		// our parent color is a number, but
		// we may have our own color:
		if(Std.is(default_style.color,String ))
			default_style.color = Utils.get_color( default_style.color );
		
		return new Horizontal( index, default_style, this.group );
	}
	
	public override function resize ( sc:ScreenCoordsBase ): Void {
		
		for ( i in 0...this.numChildren )
		{
			var p:Horizontal = cast(this.getChildAt(i),Horizontal);
			p.resize( sc );
		}
	}
	
	public override function get_max_x(): Float {
		
		var x: Float = 0;
		//
		// count the non-mask items:
		//
		for ( i in 0...this.numChildren )
			if(Std.is(this.getChildAt(i),Horizontal )) {
				
				var h:Horizontal = cast(this.getChildAt(i),Horizontal);
				x = Math.max( x, h.get_max_x() );
				
			}

		return x;
	}
	
	public override function get_min_x(): Float {
		return 0;
	}

}
